<template>
  <div class="main-header">
    <el-header height="50px">
      <div class="menu-icon" @click="handlerMenuIconClick">
        <el-icon size="28px">
          <component :is="isFold ? 'Expand' : 'Fold'"></component>
        </el-icon>
      </div>
      <div class="content">
        <div class="curmb">
          <el-breadcrumb :separator-icon="ArrowRight">
            <template v-for="item in breadcrumbs" :key="item.name">
              <el-breadcrumb-item :to="item.path">{{
                item.name
              }}</el-breadcrumb-item>
            </template>
          </el-breadcrumb>
        </div>

        <div class="header-info">
          <div class="operation">
            <span>
              <el-icon><Message /></el-icon>
            </span>
            <span>
              <el-icon><Search /></el-icon>
            </span>

            <span>
              <span class="dot"></span>
              <el-icon><ChatDotRound /></el-icon>
            </span>
          </div>

          <el-dropdown :hide-on-click="false" size="large">
            <span class="user-info">
              <el-avatar
                :size="30"
                src="https://upload.jianshu.io/users/upload_avatars/1102036/c3628b478f06.jpeg"
              />
              <span class="name">coderwhy</span>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="ExitLoginChange">
                  <el-icon><CircleClose /></el-icon>
                  退出登录
                </el-dropdown-item>
                <el-dropdown-item divided>
                  <el-icon><Warning /></el-icon>
                  个人信息
                </el-dropdown-item>
                <el-dropdown-item>
                  <el-icon><Unlock /></el-icon>
                  修改密码
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </el-header>
  </div>
</template>

<script setup lang="ts">
import router from "@/router";
import { ref, computed } from "vue";
import { localCache } from "@/utils/cache";
import { LOGIN_TOKEN } from "@/global/constants";
import { ArrowRight } from "@element-plus/icons-vue";
import { mapPathToBreadcrumbs } from "@/utils/map-menus";
import useLoginStore from "@/store/login/login";
import { useRoute } from "vue-router";

const route = useRoute();
const userMenus = useLoginStore().userMenus;
const breadcrumbs = computed(() => {
  return mapPathToBreadcrumbs(route.path, userMenus);
});

// 内部自定义事件
const emit = defineEmits(["foldChange"]);

const isFold = ref(false);
function handlerMenuIconClick() {
  // 1.改变内部状态
  isFold.value = !isFold.value;

  // 2.将事件和状态传递给父组件
  emit("foldChange", isFold.value);
}

// 退出登录
function ExitLoginChange() {
  localCache.removeCache(LOGIN_TOKEN);
  router.push("/login");
}
</script>
<style lang="less" scoped>
.main-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex: 1;
  .el-header {
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .menu-icon {
    width: 28px;

    cursor: pointer;
  }

  .content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex: 1;
    padding: 0 18px;
  }
}

.operation {
  display: inline-flex;
  margin-right: 20px;

  span {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 35px;

    &:hover {
      background: #f2f2f2;
    }

    i {
      font-size: 20px;
    }

    .dot {
      position: absolute;
      top: 3px;
      right: 3px;
      z-index: 10;
      width: 6px;
      height: 6px;
      background: red;
      border-radius: 100%;
    }
  }
}

.example-showcase .el-dropdown + .el-dropdown {
  margin-left: 15px;
}
.example-showcase .el-dropdown-link {
  cursor: pointer;
  color: var(--el-color-primary);
  display: flex;
  align-items: center;
}
.el-avatar {
  cursor: pointer;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;

  .name {
    margin-left: 5px;
  }
}
</style>
